import type { Model } from './connect';

const initalState = {
  globalRef: {},
  globalLoading: false,
};

export type GlobalModelState = Readonly<typeof initalState>;

interface GlobalModel extends Model {
  state: GlobalModelState;
}

const globalModel: GlobalModel = {
  collapsed: false,
  namespace: 'global',
  state: initalState,
  effects: {
    *fetchAllCodeList(_, { call, put, select }) {},
  },
  reducers: {
    changeLayoutCollapsed(state, { payload }) {
      return { ...state, collapsed: payload };
    },
    setGlobalRef(state, { payload }) {
      const globalRef = state.globalRef;
      const { key, ...res } = payload;
      globalRef[key] = {
        ...res,
      };
      return {
        ...state,
        globalRef,
      };
    },
    resetGlobalRef(state) {
      const globalRef = state.globalRef;
      for (const key in globalRef) {
        delete globalRef[key];
      }
      return {
        ...state,
        globalRef,
      };
    },
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  subscriptions: {},
};
export default globalModel;
